﻿using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Mitosoft.Definitions.Annotations;

namespace Mitosoft.Definitions.ViewModels
{
  /// <summary>
  /// The very basic view model.
  /// Each view model should derive from this class.
  /// </summary>
  public class BaseViewModel : INotifyPropertyChanged, IDisposable
  {
    #region events

    /// <summary>
    /// Used to inform about changed properties.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    ~BaseViewModel()
    {
      Dispose(false);
    }

    #region methods

    /// <summary>
    /// Method used to throw <see cref="PropertyChanged"/>.
    /// </summary>
    /// <param name="propertyName">Represents the name of the changed property.</param>
    [NotifyPropertyChangedInvocator]
    protected void HandlePropertyChanged([CallerMemberName]string propertyName = null)
    {
      var handler = PropertyChanged;
      if (handler == null) return;
      handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    #region disposing

    /// <summary>
    /// Gets or sets whether this object is already disposed.
    /// </summary>
    protected bool _isDisposed;

    /// <summary>
    /// Disposes this class.
    /// </summary>
    public void Dispose()
    {
      Dispose(true);

      GC.SuppressFinalize(this);
    }

    /// <summary>
    /// Disposes this object.
    /// </summary>
    /// <param name="isDisposing">Represents whether this object is disposing.</param>
    protected virtual void Dispose(bool isDisposing)
    {
      if (_isDisposed) return;

      if (isDisposing)
      {
        #region dispose managed resources

        #endregion
      }

      #region clean up unmanaged resources


      #endregion

      _isDisposed = true;
    }

    #endregion
  }
}
